 <template>
  <el-container style="height: 90vh; border: 1px solid #eee">
    <el-container>
      <el-main>
        <div id="index">
          <el-table :data="list" border style="width: 60%">
            <el-table-column fixed prop="id" label="编号" width="150">
            </el-table-column>
            <el-table-column prop="title" label="题目" width="120">
            </el-table-column>
            <el-table-column prop="author" label="作者" width="120">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="200">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  @click="$router.push(`/articles/edit/${scope.row.id}`)"
                  >编辑</el-button
                >
                <el-button type="danger" @click="open(scope)">删除</el-button>
                <!-- <el-button type="danger" @click="del(scope)">删除</el-button> -->
              </template>
            </el-table-column>
          </el-table>
          <!-- <el-card class="box-card">
      <div v-for="(item, index) in list" :key="index" class="text item">
        {{ item.title }}
        <el-button
          type="primary"
          @click="$router.push(`/articles/edit/${item.id}`)"
          >编辑</el-button
        >
        <el-button type="danger" @click="del(item)">删除</el-button>
      </div>
    </el-card> -->

          <el-pagination
            layout="prev, pager, next"
            :total="totalCount"
            :page-size="pageSize"
            @current-change="page"
          >
          </el-pagination>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 总条数，根据接口获取数据长度(注意：这里不能为空)
      totalCount: 0,
      pageSize: 0,
      list: [],
    };
  },
  methods: {
    async save() {
      const res = await this.$http.get("/indexx");
      this.list = res.data.bloglist;
      this.totalCount = res.data.count;
      this.pageSize = res.data.pagesize;
      console.log(res.data);
    },
    async page(val) {
      const res = await this.$http.get(`indexx/?page=${val}`);
      console.log(res);
      this.list = res.data.bloglist;
      console.log(val);
      this.totalCount = res.data.count;
    },
    async open(item) {
      console.log(item);
      this.$confirm(
        "此操作将永久删除" + item.row.title + ", 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          const res = this.$http.post(`article/delete?id=${item.row.id}`);
          console.log(res);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.save();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },

  created() {
    this.save();
  },
};
</script>
<style>
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 480px;
}
</style>